package com.master.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.master.entity.User;
import com.master.service.LoginService;


/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String userpassword = request.getParameter("password");
		System.out.println(username + ":"+ userpassword);
		LoginService ls = new LoginService();
		User u = ls.login(username, userpassword);
		System.out.println(u.getAID());
		if(u.getLOGIN_ID()!=null){
			request.getSession().setAttribute("curUser", u);
			request.setAttribute("username", u.getLOGIN_ID());
			RequestDispatcher rd = request.getRequestDispatcher("sys/index.jsp");
			rd.forward(request, response);
		}else{
			response.sendRedirect("login.html");
		}
	}


}
